﻿using System.Windows;
using System.Windows.Controls;
using System;
using System.Windows.Input;

namespace StockholmsStad.Windows.UI.Infrastructure.Controls
{
    /// <summary>
    /// Interaction logic for CustomDatePicker2.xaml
    /// </summary>
    public partial class CustomDatePicker2 : UserControl
    {
        public static DependencyProperty TextProperty = DependencyProperty.Register("DateValue", typeof(string), typeof(CustomDatePicker2));

        public CustomDatePicker2()
        {
            InitializeComponent();
        }

        public string DateValue
        {
            get 
            {
                return tbText.Text;// = value;
                //return (string)GetValue(TextProperty);
            }
            set 
            { 
                //SetValue(TextProperty, value);
                tbText.Text = value;
                
                try
                {
                    dateControl.SelectedDate = DateTime.Parse(tbText.Text); 
                }
                catch
                {
                    dateControl.SelectedDate = DateTime.Now;    
                }
            }
        }

        

        //private void RefreshDateValue()
        //{
        //    return;

        //    //try
        //    //{
        //    //    if(dateControl.SelectedDate != DateTime.Parse(tbText.Text))
        //    //        dateControl.SelectedDate = DateTime.Parse(tbText.Text);

        //    //    tbText.Text = dateControl.SelectedDate.HasValue ? dateControl.SelectedDate.Value.ToString("yyyy-MM-dd") : String.Empty;
        //    //}
        //    //catch
        //    //{
        //    //    dateControl.SelectedDate = DateTime.Now;
                
        //    //    if(tbText.Text != String.Empty)
        //    //        tbText.Text = String.Empty;
        //    //}
        //}

        //private void openPopup_Click(object sender, MouseButtonEventArgs e)
        //{
        //    //RefreshDateValue();

        //    popupControl.IsOpen = true;
        //}
        //private void MonthCalendar_DateSelectionChanged(object sender, Microsoft.Samples.KMoore.WPFSamples.DateControls.DateSelectionChangedEventArgs e)
        //{
        //    tbText.Text = dateControl.SelectedDate.HasValue ? dateControl.SelectedDate.Value.ToString("yyyy-MM-dd") : String.Empty;
        //    //RefreshDateValue();
        //}

        //private void tbText_KeyUp(object sender, KeyEventArgs e)
        //{
        //    try
        //    {
        //        dateControl.SelectedDate = DateTime.Parse(tbText.Text);
        //        SetValue(TextProperty, dateControl.SelectedDate.Value.ToString("yyyy-MM-dd")); 
        //    }
        //    catch
        //    {
        //    }
        //}
    }
}
